﻿using System;
using System.Windows.Forms;
using ChinaDynastyTours.Controls;
using System.Data;
using ChinaDynastyTours.Helpers;

namespace ChinaDynastyTours.Views
{
    public partial class ProviderToolForm : Form
    {
        //输入参数
        public string _CityName
        {
            get;
            set;
        }
        //输出参数
        public string ReturnProviderName { get; set; }
        public string ReturnProviderID { get; set; }

        public ProviderToolForm()
        {
            InitializeComponent();
        }

        private void HotelProviderForm_Load(object sender, EventArgs e)
        {
            //城市信息
            DataTable dtCity = BaseInfoCtrl.GetCityData();
            foreach (DataRow dr in dtCity.Rows)
            {
                tbCity.Properties.Items.Add(dr["Name"]);
            }
            tbProviderType.Properties.Items.AddRange(Enum.GetNames(typeof(ProviderTypeEnum)));

            //激发tbCity索引变更，加载数据
            tbCity.EditValue = _CityName;

            LoadProviderData(_CityName,tbProviderType.Text.Trim());
        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            if (providerGridView.DataRowCount > 0)
            {
                ReturnProviderID = providerGridView.GetDataRow(providerGridView.FocusedRowHandle)["ProviderID"].ToString();
                ReturnProviderName = providerGridView.GetDataRow(providerGridView.FocusedRowHandle)["ProviderName"].ToString();
            }
        }

        private void ProviderGridView_RowClick(object sender, DevExpress.XtraGrid.Views.Grid.RowClickEventArgs e)
        {
            if (e.Clicks == 2)
                btnOk.PerformClick();
        }

        private void tbProviderType_SelectedIndexChanged(object sender, EventArgs e)
        {
            if(tbProviderType.Text=="")return;
            LoadProviderData(tbCity.Text, tbProviderType.Text.Trim());
        }

        private void LoadProviderData(string cityName,string providerType)
        {
            providerGridControl.DataSource = BaseInfoCtrl.GetProviderData(tbCity.Text, providerType);
        }

        private void tbCity_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (tbProviderType.Text == "") return;
            LoadProviderData(tbCity.Text, tbProviderType.Text.Trim());
        }
    }
}
